package cn.javaeye.lonlysky.lforum.web.admin.web;

import java.util.List;

import org.apache.struts2.config.ParentPackage;

import cn.javaeye.lonlysky.lforum.BaseAction;
import cn.javaeye.lonlysky.lforum.comm.LForumRequest;
import cn.javaeye.lonlysky.lforum.entity.web.FartherType;
import cn.javaeye.lonlysky.lforum.service.FartherTypeManager;

@ParentPackage("default")
public class AddFartherTypeAction extends BaseAction{
	/**
	 * 文章类型
	 */
	private static final long serialVersionUID = -1840837291827904103L;
	private FartherTypeManager fartherTypeManager;
	private FartherType fartherType;
	private List<FartherType> fartherTypeList;
	protected StringBuilder htmlBuilder = new StringBuilder();
	
	@Override
	public String execute() throws Exception {
		registerAdminPageClientScriptBlock();
		String type=LForumRequest.getParamValue("type");
		if (fartherType!=null) {
			fartherType.setType(type);
			fartherTypeManager.createFartherType(fartherType);
			registerStartupScript("page", "window.location.href='addFartherType.action';");
		}
		if (LForumRequest.getParamValue("remove").equals("true")) {
			fartherTypeManager.remove(LForumRequest.getParamIntValue("id", -1));
			registerStartupScript("page", "window.location.href='addFartherType.action?type="+type+"';");
		}
		fartherTypeList=(List<FartherType>)fartherTypeManager.findByType(type);
		return SUCCESS;
	}

	public void registerStartupScript(String key, String scriptstr) {
		key = key.toLowerCase();
		if ((key.equals("pagetemplate")) || (key.equals("page"))) {
			String script = "";
			script += "<input type='hidden' name='submitMethod' value=''/>";
			script += "<script type=\"text/javascript\">function doPost(method){";
			script += "if(method!=''){document.getElementById('submitMethod').value='method:'+method;}";
			script += "var theForm = document.forms['Form1'];";
			script += "if (!theForm) {theForm = document.Form1;}";
			script += "if (!theForm.onsubmit || (theForm.onsubmit() != false)) {theForm.submit();}}</script>";
			htmlBuilder.append(script);
			
			if (key.equals("page")) {
				script = "<script> \r\n" + "var bar=0;\r\n"
						+ "document.getElementById('success').style.display = \"block\";  \r\n"
						+ "document.getElementById('Layer5').innerHTML ='<BR>操作成功执行<BR>';  \r\n" + "count() ; \r\n"
						+ "function count(){ \r\n" + "bar=bar+4; \r\n" + "if (bar<99) \r\n"
						+ "{setTimeout(\"count()\",100);} \r\n" + "else { \r\n"
						+ "document.getElementById('success').style.display = \"none\";HideOverSels('success'); \r\n"
						+ scriptstr + "} \r\n" + "} \r\n" + "</script> \r\n"
						+ "<script> window.onload = function(){HideOverSels('success')};</script>\r\n";
			}

			if (key.equals("pagetemplate")) {
				script = "<script> \r\n" + "var bar=0;\r\n success.style.display = \"block\";  \r\n"
						+ "document.getElementById('Layer5').innerHTML = '<BR>" + scriptstr + "<BR>';  \r\n"
						+ "count() ; \r\n" + "function count(){ \r\n" + "bar=bar+4; \r\n" + "if (bar<99) \r\n"
						+ "{setTimeout(\"count()\",100);} \r\n" + "else { \r\n"
						+ "document.getElementById('success').style.display = \"none\";HideOverSels('success'); \r\n"
						+ "}} \r\n" + "</script> \r\n"
						+ "<script> window.onload = function(){HideOverSels('success')};</script>\r\n";
			}
			htmlBuilder.append(script);

		} else {
			htmlBuilder.append(scriptstr);
		}
	}

	/**
	 * 注册提示信息JS脚本
	 */
	public void registerAdminPageClientScriptBlock() {
		String script = "<div id=\"success\" style=\"position:absolute;z-index:300;height:120px;width:284px;left:50%;top:50%;margin-left:-150px;margin-top:-80px;\">\r\n"
				+ "	<div id=\"Layer2\" style=\"position:absolute;z-index:300;width:270px;height:90px;background-color: #FFFFFF;border:solid #000000 1px;font-size:14px;\">\r\n"
				+ "		<div id=\"Layer4\" style=\"height:26px;background:#f1f1f1;line-height:26px;padding:0px 3px 0px 3px;font-weight:bolder;\">操作提示</div>\r\n"
				+ "		<div id=\"Layer5\" style=\"height:64px;line-height:150%;padding:0px 3px 0px 3px;\" align=\"center\"><BR /><table><tr><td valign=top><img border=\"0\" src=\"../images/ajax_loading.gif\"  /></td><td valign=middle style=\"font-size: 14px;\" >正在执行当前操作, 请稍等...<BR /></td></tr></table><BR /></div>\r\n"
				+ "	</div>\r\n"
				+ "	<div id=\"Layer3\" style=\"position:absolute;width:270px;height:90px;z-index:299;left:4px;top:5px;background-color: #E8E8E8;\"></div>\r\n"
				+ "</div>\r\n"
				+ "<script> \r\n"
				+ "document.getElementById('success').style.display = \"none\"; \r\n"
				+ "</script> \r\n" + "<script type=\"text/javascript\" src=\"../js/divcover.js\"></script>\r\n";
		htmlBuilder.append(script);
	}
	public FartherType getFartherType() {
		return fartherType;
	}

	public void setFartherType(FartherType fartherType) {
		this.fartherType = fartherType;
	}

	public FartherTypeManager getFartherTypeManager() {
		return fartherTypeManager;
	}
	public void setFartherTypeManager(FartherTypeManager fartherTypeManager) {
		this.fartherTypeManager = fartherTypeManager;
	}

	public List<FartherType> getFartherTypeList() {
		return fartherTypeList;
	}

	public void setFartherTypeList(List<FartherType> fartherTypeList) {
		this.fartherTypeList = fartherTypeList;
	}

	public StringBuilder getHtmlBuilder() {
		return htmlBuilder;
	}

	public void setHtmlBuilder(StringBuilder htmlBuilder) {
		this.htmlBuilder = htmlBuilder;
	}
}
